/*
* This software is licensed under the GNU General Public License, version 2
* You may copy, distribute and modify the software as long as you track changes/dates of in source files and keep all modifications under GPL. You can distribute your application using a GPL library commercially, but you must also provide the source code.

* DNNspot Software (http://www.dnnspot.com)
* Copyright (C) 2013 Atriage Software LLC
* Authors: Kevin Southworth, Matthew Hall, Ryan Doom

* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

* Full license viewable here: http://www.gnu.org/licenses/gpl-2.0.txt
*/

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DNNspot.LiveChat.DataModel;
using DotNetNuke.Common;
using DotNetNuke.Entities.Host;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security;

namespace DNNspot.LiveChat
{
    public partial class Default : ModuleBase
    {
        // ### EVENTS ######################################################

        protected void Page_PreRender(object sender, EventArgs e)
        {
            IncludeJS();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            HandlePostBack();

            if (!IsPostBack)
            {
                LoadControls();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            PortalSetting.Set(PortalId, PortalSettingNames.WelcomeMessage, txtWelcomeMessage.Text);
            PortalSetting.Set(PortalId, PortalSettingNames.ChatOnlineMessage, txtChatOnlineMessage.Text);
            PortalSetting.Set(PortalId, PortalSettingNames.ChatOfflineMessage, txtChatOfflineMessage.Text);
            PortalSetting.Set(PortalId, PortalSettingNames.QueueTimeout, txtQueueTimeout.Text);
            PortalSetting.Set(PortalId, PortalSettingNames.QueueTimeoutMessage, txtQueueTimeoutMessage.Text);
        }

        // ### PRIVATE FUNCTIONS ###########################################

        private void HandlePostBack()
        {
            var target = Request.Form["__EVENTTARGET"];
            var argument = Request.Form["__EVENTARGUMENT"];

            if (target == PostBackEvents.EditAgent)
            {
                var agentId = Convert.ToInt32(argument);
                Response.Redirect(Globals.NavigateURL(TabId, ControlKeys.EditAgent, new[] {
                    string.Format("mid={0}", ModuleId),
                    string.Format("{0}={1}", Params.AgentId, agentId)
                }));
            }
            else if (target == PostBackEvents.EditCategory)
            {
                var categoryId = Convert.ToInt32(argument);
                Response.Redirect(Globals.NavigateURL(TabId, ControlKeys.EditCategory, new[] {
                    string.Format("mid={0}", ModuleId),
                    string.Format("{0}={1}", Params.CategoryId, categoryId)
                }));
            }
            else if (target == PostBackEvents.EditSession)
            {
                var sessionId = Convert.ToInt32(argument);
                Response.Redirect(Globals.NavigateURL(TabId, ControlKeys.EditSession, new[] {
                    string.Format("mid={0}", ModuleId),
                    string.Format("{0}={1}", Params.SessionId, sessionId)
                }));
            }
        }

        private void IncludeJS()
        {
            //if (string.IsNullOrEmpty(HostSettings.GetHostSetting(ModuleSettings.NoJQuery)) && HttpContext.Current.Items["jquery_registered"] == null)
            //{
            //    RegisterJavascript(String.Format("{0}{1}js/jquery-1.3.1.min.js", Globals.ApplicationPath, ModuleWebPath));
            //    HttpContext.Current.Items.Add("jquery_registered", "true");
            //}

            RegisterJavascript(string.Format("{0}{1}js/dnnspot.livechat.admin.include.js", Globals.ApplicationPath, ModuleWebPath));
        }

        private void LoadControls()
        {
            txtWelcomeMessage.Text = PortalSetting.Get(PortalId, PortalSettingNames.WelcomeMessage) ?? "Waiting for agent to connect...";
            txtChatOnlineMessage.Text = PortalSetting.Get(PortalId, PortalSettingNames.ChatOnlineMessage) ?? "Live Chat";
            txtChatOfflineMessage.Text = PortalSetting.Get(PortalId, PortalSettingNames.ChatOfflineMessage) ?? "Live Chat Offline";
            txtQueueTimeout.Text = PortalSetting.Get(PortalId, PortalSettingNames.QueueTimeout) ?? "180";
            txtQueueTimeoutMessage.Text = PortalSetting.Get(PortalId, PortalSettingNames.QueueTimeoutMessage) ?? null;

            SetChatLink();
        }

        private void SetChatLink()
        {
            if (Agent.IsAgent(UserId))
            {
                lnkChat.NavigateUrl = string.Format("{0}AgentChat.aspx", ModuleWebPath);
            }
            else
            {
                lnkChat.Visible = false;
                pnlSignIntoDifferentAccount.Visible = true;
            }
        }
    }
}

